package com.peace.hospital.controller;


import com.alibaba.fastjson.JSON;

import com.alibaba.fastjson.serializer.SerializerFeature;

import com.peace.hospital.entity.common.Druginformation;

import com.peace.hospital.entity.drugstorage.*;
import com.peace.hospital.service.DrugStoProService;
import com.peace.hospital.service.common.IDruginformationService;
import com.peace.hospital.service.common.IStaffinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


import java.util.List;

@Controller
@RequestMapping("/drugStorageController")
public class DrugStorageController {

    @Autowired
    private IDruginformationService iDruginformationService;

    @Autowired
    private DrugStoProService drugStoProService;

    @Autowired
    private IStaffinfoService iStaffinfoService;


    //添加药品信息
    @PostMapping("/addDrugInfo")
    @ResponseBody
    public String addDrugInfo(@RequestBody Druginformation din) {

        System.out.println("仓库微服务，添加药品信息");

//      设置药品信息


        System.out.println(din);

//        调用添加药品的方法
        boolean drugBool = iDruginformationService.save(din);
        if(drugBool){
            return "success";
        }else{
            return "fail";
        }


    }

    //查询药品信息
    @GetMapping("/findDrugInfo")
    @ResponseBody
    public String findDrugInfo(@RequestBody Druginformation din) {
        Druginformation din1 = drugStoProService.findDrugInfo(din);

        System.out.println(JSON.toJSONStringWithDateFormat(din1, "YYYY-MM-DD", SerializerFeature.WriteDateUseDateFormat));

        return JSON.toJSONStringWithDateFormat(din1, "YYYY-MM-DD", SerializerFeature.WriteDateUseDateFormat);
    }

    //修改药品信息
    @PostMapping("/alterDrugInfo")
    @ResponseBody
    public String alterDrugInfo(@RequestBody Druginformation din) {
        System.out.println("生产者控制层 修改药品信息：" + din);
        int i = drugStoProService.updateDrug(din);
        System.out.println(i);
        if (i > 0) {

            return "success";
        }
        return "fail";
    }

    //添加入库单
    @PostMapping("/addWaeInfo")
    @ResponseBody

    public String addWaeInfo(@RequestBody DruginfoAndAnyDto druginfoAndAnyDto) {

        System.out.println("开始添加入库单");
        Waentry waentry=druginfoAndAnyDto.getWaentry();

        System.out.println(waentry);
        //        调用添加入库单的方法


        int i=drugStoProService.addWaentry(waentry);
    //插入失败 返回fail
        if(i==0){
            return "fail";
        }
        List<Druginformation> listDrug = druginfoAndAnyDto.getDruginformation();
//        获取添加入库单的id
        int waeId=drugStoProService.findLastId();
        for (Druginformation druginformation : listDrug) {

            System.out.println("药品：" + druginformation);
//            调用添加药品信息的方法 ，添加失败 返回 fail
            if(iDruginformationService.save(druginformation)==false){
                return "fail";
            }
            int drugId=drugStoProService.findDruginLastId();

//           调用添加 入库和药品信息的中间表 的方法，添加失败 返回fail
            if(drugStoProService.addDruAndWa(drugId,waeId)==0){

                return "fail";
            }

        }
        return "success";
    }
// 查询入库单
    @GetMapping("/findWaentry")
    @ResponseBody
    public String findWaentry(@RequestBody Waentry waentry){

        System.out.println("进入消费者 控制层");
        System.out.println("waentry:"+waentry);
        Waentry waentry1=null;
//            判定前台传来的值
        if(waentry.getWaedate()==null){
//            调用查询入库单信息的方法
            waentry1=drugStoProService.findWaeById(waentry.getWaeid());
            System.out.println(waentry1);
            System.out.println(JSON.toJSONString(waentry1));
            return JSON.toJSONString(waentry1);
        }else{
            System.out.println("else");
//            调用 查询入库人名字的方法
            List<Waentry> waentryList=drugStoProService.findWaeByDateAndStaid(waentry);
            for(Waentry waentry2:waentryList){
                System.out.println(waentry2);
            }
            System.out.println(JSON.toJSONString(waentryList));
            return JSON.toJSONString(waentryList);
        }
    }
//查询入库的药品
    @GetMapping("/findDrugByWaeId")
    @ResponseBody
    public String findDrugByWaeId(@RequestBody Waentry waentry){
        System.out.println("进入生产者 控制层："+waentry);
//        调用查询入库药品的方法
        List<Druginformationandwaentry> druginformationandwaentryList=drugStoProService.findDrugByWaeId(waentry);
//        遍历list集合
        for(Druginformationandwaentry drugin:druginformationandwaentryList){
            System.out.println(drugin);
        }
        return JSON.toJSONString(druginformationandwaentryList);
    }
// 插入报损单 和报损药品信息
    @PostMapping("/addDamage")
    @ResponseBody
    public String addDamage(@RequestBody DruginfoAndAnyDto druginfoAndAnyDto){
        Frmloss frmloss=druginfoAndAnyDto.getFrmloss();
        frmloss.setFrmdbsource("drugstorage");
//        调用插入报损单信息的方法
        int i=drugStoProService.addFromloss(frmloss);
        if(i==0){
            return "fail";
        }
//        调用查询 最新一条报损单的方法
        Frmloss frmloss1=drugStoProService.findFrmloss();
        System.out.println("查询到最新的一套报损单："+frmloss1);
        for(Damagedrug damagedrug:druginfoAndAnyDto.getDamagedrug()){
            damagedrug.setFrmid(frmloss1.getFrmid());
            int j=drugStoProService.addDamageDrug(damagedrug);
            if(j==0){
                return "fail";
            }
        }
        return "success";
    }
//查询报损单
    @GetMapping("/findFrmByfrmid")
    @ResponseBody
    public String findFrmByFrmid(@RequestBody Frmloss frmloss){
        System.out.println(frmloss);
        List<Frmloss> frmloss1=drugStoProService.findFrmByFrmid(frmloss);
        System.out.println("出来吧皮卡丘："+JSON.toJSONString(frmloss1));
        return JSON.toJSONString(frmloss1);
    }
//查询报损药品
    @GetMapping("/findDamageByFrmid")
    @ResponseBody
    public String findDamageByFrmid(@RequestBody Damagedrug damagedrug){
        List<Damagedrug> damagedrugList=drugStoProService.findDamageById(damagedrug);
        System.out.println(JSON.toJSONString(damagedrugList));
        return JSON.toJSONString(damagedrugList);
    }
    @GetMapping("/test")
    @ResponseBody
    public String test(){
        return "haha";
    }

//    添加出库信息
    @PostMapping("/addOutWInfo")
    @ResponseBody
    public String addOutWInfo(@RequestBody DruginfoAndAnyDto druginfoAndAnyDto){

        Outwarehouse outwarehouse=druginfoAndAnyDto.getOutwarehouse();
        int i=drugStoProService.addOutwa(outwarehouse);
        if(i==0){
            System.out.println("i="+i);
            return "fail";

        }else{
            Outwarehouse outwarehouse1=drugStoProService.findLastOutwa();
            for(Outdrug outdrug:druginfoAndAnyDto.getOutdrug()){

                outdrug.setOutid(outwarehouse1.getOutid());
                int j=drugStoProService.addOutDrug(outdrug);
                if(j==0){
                    System.out.println("j="+j);
                    return "fail";
                }else{
                    int m=drugStoProService.updateDruginfo(outdrug);
                    if(m==0){
                        System.out.println("m="+m);
                        return "fail";
                    }
                }
            }
            return "success";
        }



    }
//    查找出库单信息
    @GetMapping("/findOutWa")
    @ResponseBody
    public String findOutWa(@RequestBody Outwarehouse outwarehouse){
        List<Outwarehouse> outwarehouseList=drugStoProService.findOutwa(outwarehouse);

        return JSON.toJSONString(outwarehouseList);
    }
//    出库单id查询出库药品
    @GetMapping("/findOutDByOutid")
    @ResponseBody
    public String findOutDByOutid(@RequestBody Outdrug outdrug){
        List<Outdrug> outdrugList=drugStoProService.findOutDByOutid(outdrug);
        System.out.println(JSON.toJSONString(outdrugList));
        return JSON.toJSONString(outdrugList);
    }
}
